package com.atguigu.datasource;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Calendar;
import java.util.Random;

public class RandomWatersensor implements SourceFunction<WaterSensor> {

    //定义私有的零时判断变量
    private boolean flag = true;

    //自定数据源的执行方法
    @Override
    public void run(SourceContext<WaterSensor> ctx) throws Exception {
        //自己创建一些数据
        Random random = new Random();

        while(flag){
            ctx.collect(new WaterSensor(
                    "sensor" + random.nextInt(50),
                    Calendar.getInstance().getTimeInMillis(),
                    random.nextInt(100)
            ));

            //间隔1秒造一个数据
            Thread.sleep(1000);
        }


    }

    @Override
    public void cancel() {

    }
}
